package edu.hawaii.myisern.action;


import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;

/**
 * Tests the organization form Action Bean.
 *
 * @author Kevin English
 */
public class TestOrganizationFormActionBean extends TestWeb {
  private static final String EDIT_ADD_FORM_PAGE_TITLE = "ISERN - Edit/Add Organization";
  private static final String TEST_NAME = "Updated odfdfdfrg name";
  private static final String NAME_FORM_FIELD = "organization.name";
  private static final String FORM_CSS_ID = "MyForm";
  private static final String EDIT_LINK = "Edit";
  private static final String JSP_TABLE_LIST = "organizations.jsp";
  private static final String TEST_ENTITY_NAME = "Organization";
  private static final String NEW_LINK = "organization_form.jsp";
  private static final String TEST_NEW_NAME = "My New Org II ";
  private static final String EDIT_URL_QS_PREFIX = 
    "OrganizationForm.action?preEdit=&_sourcePage=%2Forganizations.jsp&id=";
  /**
   * Tests that the  organization form requires a name.
   *    
   * @throws Exception If problems occur
   */
  @Test
  public void testRequiresName() throws Exception {
    assertNotNull("the user session should not be null", 
        conversation.getCookieValue("JSESSIONID"));
    WebResponse response = conversation.getResponse(getAppUrl() + "/" + JSP_TABLE_LIST );
    WebLink editLink = response.getLinkWith(EDIT_LINK);
    response = editLink.click();
    assertEquals("Check clicking edit forwards to edit page.", 
        EDIT_ADD_FORM_PAGE_TITLE, response.getTitle());
    WebForm form = response.getFormWithID(FORM_CSS_ID); 
    WebRequest request = form.getRequest();
    request.setParameter(NAME_FORM_FIELD, "");
    response = conversation.getResponse(request);
    assertEquals("Check that error message is displayed.", true, 
        response.getText().contains("Name is a required field"));    
    
  }
  /**
   *  Tests that a succssful save.
   *
   * @throws Exception If problems occur
   */
  @Test
  public void testSuccess() throws Exception {
    assertNotNull("the user session should not be null",
        conversation.getCookieValue("JSESSIONID") );
    WebResponse response = conversation.getResponse(getAppUrl() + "/" + JSP_TABLE_LIST);
    WebLink editLink = response.getLinkWith(EDIT_LINK);
    response = editLink.click();
    assertEquals("Check clicking edit forwards to proper page.", 
        EDIT_ADD_FORM_PAGE_TITLE, response.getTitle());
    WebForm form = response.getFormWithID(FORM_CSS_ID); 
    WebRequest request = form.getRequest();
    
    String oldName = form.getParameterValue(NAME_FORM_FIELD);
    String newName = TEST_NAME;
    
    request.setParameter(NAME_FORM_FIELD, newName);
    response = conversation.getResponse(request);
    assertEquals("Check " + TEST_ENTITY_NAME + " name is updated.", true, 
        response.getText().contains(newName));   
    
    response = conversation.getResponse(getAppUrl() + "/" +  EDIT_URL_QS_PREFIX + newName );
    form = response.getFormWithID(FORM_CSS_ID);
    request = form.getRequest();
    request.setParameter(NAME_FORM_FIELD, oldName);

    response = conversation.getResponse(request);
    assertEquals("Check " + TEST_ENTITY_NAME + " name is updated.", true, 
        response.getText().contains(oldName));   
  }
  /**
   *  Tests a create.
   *
   * @throws Exception If problems occur
   */
  @Test
  public void testCreate() throws Exception {
    assertNotNull("the user session should not be null",
        conversation.getCookieValue("JSESSIONID") );
    WebResponse response = conversation.getResponse(getAppUrl() + "/" + NEW_LINK);
    assertEquals("Check clicking logout forwards to edit page.", 
        EDIT_ADD_FORM_PAGE_TITLE, response.getTitle());
    WebForm form = response.getFormWithID(FORM_CSS_ID); 
    WebRequest request = form.getRequest();

    String newName = TEST_NEW_NAME;
    request.setParameter(NAME_FORM_FIELD, newName);
    response = conversation.getResponse(request);
    assertEquals("Check " + TEST_ENTITY_NAME + " name is updated.", true, 
        response.getText().contains(newName));
  }    
}
